﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.CustomerManage;
using MINERP.COM;

namespace MINERP.CustomerManage.StartOrder
{
    public partial class FrmCheckSizeAndPageEdit : Form
    {
        public FrmCheckSizeAndPageEdit()
        {
            InitializeComponent();
        }

        public FrmCheckSizeAndPageEdit(StartOrderBLL.ProductInfoEdit inProduct)
        {
            InitializeComponent();
            labelProductName.Text = inProduct.ProductName;
            product = inProduct;
        }
        StartOrderBLL.ProductInfoEdit product = null;
        public string SizeUnit
        {
            get
            {
                return cbUnit.Text.Trim();
            }
            set
            {
                cbUnit.Text = value;
            }
        }

        public double Width
        {
            get
            {
                return Convert.ToDouble(spinWidth.Tag);
            }
            set
            {
                spinWidth.Tag = (decimal)value;
            }
        }
        public double Height
        {
            get
            {
                return Convert.ToDouble(spinHeight.Tag);
            }
            set
            {
                spinHeight.Tag = (decimal)value;
            }
        }

        public int PageNum
        {
            get
            {
                return Convert.ToInt32(spinEditPageNum.Value);
            }
            set
            {
                spinEditPageNum.Value = (decimal)value;
            }
        }


        private void cbUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {

                float rh = (float)(Convert.ToDouble(spinHeight.Tag));
                float rw = (float)(Convert.ToDouble(spinWidth.Tag));
                float h = 0;
                float w = 0;
                string unit = cbUnit.Text;
                switch (unit)
                {
                    case "厘米(cm)":
                        h = rh * (float)2.54;
                        w = rw * (float)2.54;
                        break;
                    case "英寸(in)":
                        h = rh;
                        w = rw;
                        break;
                    case "米(m)":
                        h = rh * (float)2.54 / 100;
                        w = rw * (float)2.54 / 100;
                        break;
                    case "毫米(mm)":
                        h = rh * (float)2.54 * 10;
                        w = rw * (float)2.54 * 10;
                        break;
                }
                spinHeight.Text = string.Format("{0:0.##}", h);
                spinWidth.Text = string.Format("{0:0.##}", w);                                                         //按单位设置产品的高宽

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void spinHeight_EditValueChanged(object sender, EventArgs e)
        {
            string unit = cbUnit.Text;
            switch (unit)
            {
                case "厘米(cm)":
                    spinHeight.Tag = (double)spinHeight.Value / 2.54;

                    break;
                case "英寸(in)":
                    spinHeight.Tag = (double)spinHeight.Value;
                    break;
                case "米(m)":
                    spinHeight.Tag = (double)spinHeight.Value * 100 / 2.54;
                    break;
                case "毫米(mm)":
                    spinHeight.Tag = (double)spinHeight.Value / 100 / 2.54;
                    break;
            }
        }

        private void spinWidth_EditValueChanged(object sender, EventArgs e)
        {
            string unit = cbUnit.Text;
            switch (unit)
            {
                case "厘米(cm)":
                    spinWidth.Tag = (double)spinWidth.Value / 2.54;

                    break;
                case "英寸(in)":
                    spinWidth.Tag = (double)spinWidth.Value;
                    break;
                case "米(m)":
                    spinWidth.Tag = (double)spinWidth.Value * 100 / 2.54;
                    break;
                case "毫米(mm)":
                    spinWidth.Tag = (double)spinWidth.Value / 100 / 2.54;
                    break;
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;

            this.Close();
        }

        private void FrmCheckSizeAndPage_Load(object sender, EventArgs e)
        {
            this.SizeUnit = "厘米(cm)";
            switch (product.ProductTypeCode)
            {
                case (int)OrderInfoProductType.NeedDesignPrint:
                    spinHeight.Enabled = true; 
                    spinWidth.Enabled = true;
                    spinEditPageNum.Enabled = true;
                    break;
                case (int)OrderInfoProductType.NeedDesignProduct:
                    spinHeight.Enabled = false;
                    spinWidth.Enabled = false;
                    spinEditPageNum.Enabled = true;
                    break;
                default:
                    spinHeight.Enabled = false;
                    spinWidth.Enabled = false;
                    spinEditPageNum.Enabled = true;
                    break;

            }
           
            float h = (float)this.Height;
            float w = (float)this.Width;
            string unit = this.SizeUnit;
            switch (unit)
            {
                case "厘米(cm)":
                    h = h * (float)2.54;
                    w = w * (float)2.54;
                    break;
                case "英寸(in)":
                    h = h;
                    w = w;
                    break;
                case "米(m)":
                    h = h * (float)2.54 / 100;
                    w = w * (float)2.54 / 100;
                    break;
                case "毫米(mm)":
                    h = h * (float)2.54 * 10;
                    w = w * (float)2.54 * 10;
                    break;
            }
            spinHeight.Text = string.Format("{0:0.##}", h);
            spinWidth.Text = string.Format("{0:0.##}", w);      
        }





    }
}
